package com.mypack.dao;

import java.util.List;

import org.guzz.Guzz;
import org.guzz.dao.GuzzBaseDao;
import org.guzz.orm.se.SearchExpression;
import org.guzz.orm.se.Terms;
import org.guzz.orm.sql.CompiledSQL;
import org.guzz.transaction.ReadonlyTranSession;
import org.guzz.transaction.TransactionManager;

import com.mypack.business.User;

@SuppressWarnings("unchecked")
public class UserDAO extends GuzzBaseDao {

	public Long saveUser(User user) {
		Long id = (Long) super.insert(user, user.getSellerId());
		return id;
	}

	public User getUser(User user) {
		Guzz.setTableCondition(user.getSellerId());
		return (User) super.getForRead(User.class, user.getId());
	}

	public List<User> queryUser(User user) {
		SearchExpression se = SearchExpression.forClass(User.class);
		se.and(Terms.eq("sellerId", user.getSellerId()));
		se.setTableCondition(user.getSellerId());
		return (List<User>) super.list(se);
	}

	public List<User> queryUserByCompiledSQL(User user) {
		TransactionManager tm = super.getTransactionManager();

		String sql = "select * from @@" + User.class.getName()
				+ " where seller_id = :sellerId";

		CompiledSQL cs = tm.getCompiledSQLBuilder().buildCompiledSQL(
				User.class, sql);

		ReadonlyTranSession session = tm.openNoDelayReadonlyTran();
		List<User> userList = session.list(cs.bind("sellerId",
				user.getSellerId()));
		session.close();
		return userList;
	}
}
